package com.ruoyi.web.controller.system.fuw;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.web.controller.util.DataUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fuw.domain.FuwComm;
import com.ruoyi.fuw.service.IFuwCommService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品信息Controller
 * 
 * @author ruoyi
 * @date 2021-10-09
 */
@RestController
@RequestMapping("/fuw/comm")
public class FuwCommController extends BaseController
{
    @Autowired
    private IFuwCommService fuwCommService;

    /**
     * 查询商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:comm:list')")
    @GetMapping("/list")
    public TableDataInfo list(FuwComm fuwComm)
    {
        startPage();
        List<FuwComm> list = fuwCommService.selectFuwCommList(fuwComm);
        return getDataTable(list);
    }

    /**
     * 通过commList的commId获取商品的详细信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:comm:list')")
    @GetMapping("/list/car")
    public AjaxResult listCarComm(FuwComm fuwCommString)
    {
        // 定义商品详情List
        List<FuwComm> fuwCommList = new ArrayList<>();

        String[] fuwCarList = fuwCommString.getCommId().split(",");

        for(String fuwCarString: fuwCarList){
            FuwComm fuwComm = fuwCommService.selectFuwCommByCommId(fuwCarString);
            fuwCommList.add(fuwComm);
        }

        return AjaxResult.success(fuwCommList);
    }

    /**
     * 导出商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:comm:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FuwComm fuwComm)
    {
        List<FuwComm> list = fuwCommService.selectFuwCommList(fuwComm);
        ExcelUtil<FuwComm> util = new ExcelUtil<FuwComm>(FuwComm.class);
        return util.exportExcel(list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:comm:query')")
    @GetMapping(value = "/{commId}")
    public AjaxResult getInfo(@PathVariable("commId") String commId)
    {
        return AjaxResult.success(fuwCommService.selectFuwCommByCommId(commId));
    }

    /**
     * 新增商品信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:comm:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(MultipartFile file,
                          @RequestParam("commId") String commId,
                          @RequestParam("commName") String commName,
                          @RequestParam("commText") String commText,
                          @RequestParam("commType") String commType,
                          @RequestParam("commDanjia") BigDecimal commDanjia,
                          @RequestParam("commZhekou") Integer commZhekou,
                          @RequestParam("commKucun") Long commKucun)
    {
        // 根据文件路径上传
        String fullPath = "";
        if(file != null){
            try {
                fullPath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            fullPath = "http://localhost/dev-api" + fullPath;
        }

        // 自定义格式UI
        String uuidStr = DataUtils.fuwUUID("Comm");

        // 定义类
        FuwComm fuwComm = new FuwComm();
        fuwComm.setCommId(uuidStr);
        fuwComm.setCommName(commName);
        fuwComm.setCommText(commText);
        fuwComm.setCommPicture(fullPath);
        fuwComm.setCommType(commType);
        fuwComm.setCommDanjia(commDanjia);
        fuwComm.setCommZhekou(commZhekou);
        fuwComm.setCommKucun(commKucun);

        return toAjax(fuwCommService.insertFuwComm(fuwComm));
    }

    /**
     * 修改商品信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:comm:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update")
    public AjaxResult edit(@RequestParam("fileNow") Boolean fileNow,
                           MultipartFile file,
                           @RequestParam("commId") String commId,
                           @RequestParam("commName") String commName,
                           @RequestParam("commText") String commText,
                           @RequestParam("commType") String commType,
                           @RequestParam("commDanjia") BigDecimal commDanjia,
                           @RequestParam("commZhekou") Integer commZhekou,
                           @RequestParam("commKucun") Long commKucun)
    {
        // 定义类
        FuwComm fuwComm = fuwCommService.selectFuwCommByCommId(commId);

        if(fileNow){
            // 根据文件路径上传
            String fullPath = "";
            if(file != null){
                try {
                    fullPath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                fullPath = "http://localhost/dev-api" + fullPath;
            }
            fuwComm.setCommPicture(fullPath);
        }

        // 定义类
        fuwComm.setCommName(commName);
        fuwComm.setCommText(commText);
        fuwComm.setCommType(commType);
        fuwComm.setCommDanjia(commDanjia);
        fuwComm.setCommZhekou(commZhekou);
        fuwComm.setCommKucun(commKucun);

        return toAjax(fuwCommService.updateFuwComm(fuwComm));
    }

    /**
     * 删除商品信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:comm:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commIds}")
    public AjaxResult remove(@PathVariable String[] commIds)
    {
        return toAjax(fuwCommService.deleteFuwCommByCommIds(commIds));
    }
}
